package com.cet.qz.unit01.chapter1_3;

/**
 * @program: algorithm
 * @description: 链表节点相关操作
 * @author: 陈恩涛
 * @create: 2021-04-13 21:23
 **/
public class Qz1_3_24 {

    private static class Node<Item> {
        private Item item;
        private Node<Item> next;
    }

    /**
     *方法描述： 删除节点后续节点
     *
     * @param node
     * @return void
     * @author chenentao
     * @date 2021/4/13 22:04
     */
    private static void removeAfter(Node node) {
        if (node != null && node.next != null) {
            node.next = node.next.next;
        }
    }

    /**
     *方法描述： 将第二个节点插入链表并成为第一个节点的后续节点
     *
     * @param node1
     * @param node2
     * @return void
     * @author chenentao
     * @date 2021/4/13 22:10
     */
    private static void insertAfter(Node node1,Node node2) {
        if (node1 == null || node2 == null) {
            return;
        }
        node2.next = node1.next;
        node1.next = node2;
    }

    private static void printLink(Node node) {
        Node tmp = node;
        while (tmp != null) {
            System.out.println(tmp.item);
            tmp = tmp.next;
        }
    }

    public static void main(String[] args) {
        Node<Integer> node1 = new Node<>();
        node1.item = 1;
        Node<Integer> node2 = new Node<>();
        node2.item = 2;
        Node<Integer> node3 = new Node<>();
        node3.item = 3;
        Node<Integer> node4 = new Node<>();
        node4.item = 4;

        node1.next = node2;
        node2.next = node3;
        printLink(node1);
        removeAfter(node1);
        removeAfter(node3);
        System.out.println("==========================");
        printLink(node1);
        System.out.println("==========================");
        insertAfter(node3, node4);
        printLink(node1);

    }
}
